package command.webServices
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	import com.adobe.webapis.flickr.*;
	import com.adobe.webapis.flickr.events.*;
	
	import event.WebServiceEvent;
	
	import flash.events.Event;
	
	import model.Model;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
	/**
	 * kvdsvsdl
	 */ 
	public class GetFlickerCommand implements ICommand, IResponder
	{
		
		private var model:Model = Model.instance;
		
		public function GetFlickerCommand()
		{
		}
		
		public function execute(evento:CairngormEvent):void
		{
			
			var location:String;
			var service:HTTPService = new HTTPService();
			service.method = "GET";
			service.resultFormat="e4x";
			location= "lat=" + evento.data.lat + "&lon=" + evento.data.lng;
				
			//service.url = "http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=c51b2b0b78fe2cbc841548e6c697ee99&lat=41.95995576198951&lon=12.431506031250006&radius=10";
			service.url = "http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=c51b2b0b78fe2cbc841548e6c697ee99&"+ location +"&radius=10";																																						
			service.addEventListener(ResultEvent.RESULT, result);
			service.addEventListener(FaultEvent.FAULT, fault);
			service.send();
			
		}
		
		public function result(data:Object):void
		{
			var photoList:PagedPhotoList;
			photoList = new PagedPhotoList();
			var resultXML    : XML = XML(data.result);
			//var resul_xml_list:XMLList = resultXML..photo;			
			//model.Model.instance.flicker_xml= resultXML..photos; // TIENE KE SER PHOTO LIST.
			photoList = convert2PagedPhotoList(resultXML);
			model.Model.instance.flicker_xml= photoList.photos;
			trace("flicker-xml" + model.Model.instance.flicker_xml);
			
					
		}
		/*** private functions to parse the flicker xml file *****/
		private function convert2PagedPhotoList( xml:XML ):PagedPhotoList 
		{ 
			var pagedPhotoList:PagedPhotoList = new PagedPhotoList(); 
			pagedPhotoList.page = parseInt( xml..photos.@page ); 
			pagedPhotoList.pages = parseInt( xml.photos.@pages ); 
			pagedPhotoList.perPage = parseInt( xml.photos.@perpage ); 
			pagedPhotoList.total = parseInt( xml.photos.@total ); 
			
			var photos:Array = new Array(); 
			for each ( var p:XML in xml.photos.photo ) { 
				var photo:Photo = new Photo(); 
				photo.notes = new Array(); 
				photo.notes[0] = p..@title.toString();
				photo.id = p..@id.toString(); 
				photo.ownerId = p..@owner.toString(); 
				photo.secret = p..@secret.toString(); 
				photo.server = parseInt( p.@server ); 
				photo.title = p..@title.toString(); 
				photo.isPublic = p..@ispublic.toString() == "1"; 
				photo.isFriend = p..@isfriend.toString() == "1"; 
				photo.isFamily = p..@isfamily.toString() == "1";
				if ( p..@license.toString() ) { 
					photo.license = parseInt( p.@license ); 
				} 
				photo.dateUploaded = stringToDate( p..@dateupload.toString() ); 
				photo.dateTaken = stringToDate( p..@datetaken.toString() ); 
				photo.dateAdded = stringToDate( p..@dateadded.toString() ); 
				photo.ownerName = p..@ownername.toString(); 
				if ( p..@iconserver.toString() ) { 
					photo.iconServer = parseInt( p.@iconserver ); 
				} 
				photo.originalFormat = p..@originalformat.toString(); 
				photos.push( photo ); 
				
			}	
			pagedPhotoList.photos = photos;
			
			return pagedPhotoList; 
		} 
		/*** private functions to parse string flickerDate to a Date type *****/
		internal static function stringToDate( str:String = "" ):Date {
			if ( str == "" ) {
				return null;
			}
			
			var date:Date = new Date();
			// split the date into date / time parts
			var parts:Array = str.split( " " );
			
			// See if we have the xxxx-xx-xx xx:xx:xx format
			if ( parts.length == 2 ) {
				var dateParts:Array = parts[0].split( "-" );
				var timeParts:Array = parts[1].split( ":" );
				
				date.setFullYear( dateParts[0] );
				date.setMonth( dateParts[1] - 1 ); // subtract 1 (Jan == 0)
				date.setDate( dateParts[2] );
				date.setHours( timeParts[0] );
				date.setMinutes( timeParts[1] );
				date.setSeconds( timeParts[2] );
				
			} else {
				// Create a date based on # of seconds since Jan 1, 1970 GMT
				date.setTime( parseInt( str ) * 1000 );
			}
			
			return date;
		}
		
		public function fault(info:Object):void
		{
			Alert.show("Fault");
			
		}
	}
}